import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:music_app/controllers/audio_player_controller.dart';
import 'package:music_app/widget/my_cached_network_image.dart';
import 'package:music_app/widget/realistic_playing_indicator.dart';
import 'package:music_app/widget/song_action_bottom_sheet.dart';

class ListSongItem extends StatelessWidget {
// fee 值	含义
// 0	免费歌曲
// 1	VIP 歌曲（需要付费或会员）
// 4	需要购买数字专辑
// 8	试听片段，完整听需要 VIP（你提供的歌曲就是这种）
// 1000	无版权
  final int id;
  final String imageUrl;
  final String title;
  final String artist;
  final int fee;
  final VoidCallback? onTap;

  const ListSongItem({
    super.key,
    required this.imageUrl,
    required this.title,
    required this.artist,
    this.onTap,
    required this.id,
    required this.fee,
  });

  @override
  Widget build(BuildContext context) {
    // 音乐播放控制方法
    final audioController = Get.find<AudioPlayerController>();
    return InkWell(
      excludeFromSemantics: true,
      onTap: onTap,
      child: Padding(
        padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 16),
        child: Row(
          children: [
            Obx(() {
              final isPlaying = audioController.currentSongId == id &&
                  audioController.isPlaying.value;
              return Stack(
                alignment: Alignment.center,
                children: [
                  ClipRRect(
                    borderRadius: BorderRadius.circular(8),
                    child: MyCachedNetworkImage(
                      imageUrl: imageUrl,
                      width: 48,
                      height: 48,
                      x: 192,
                      y: 192,
                    ),
                  ),
                  if (isPlaying) ...[
                    // 黑色遮罩
                    Container(
                      width: 48,
                      height: 48,
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(8),
                        color: Colors.black.withOpacity(0.4),
                      ),
                    ),
                    // 播放动画
                    const RealisticPlayingIndicator(
                      barCount: 3,
                      width: 3,
                      height: 20,
                      color: Colors.white,
                    ),
                  ],
                ],
              );
            }),
            const SizedBox(width: 12),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text(
                    title,
                    style: const TextStyle(
                        fontSize: 14, fontWeight: FontWeight.bold),
                    overflow: TextOverflow.ellipsis,
                  ),
                  const SizedBox(height: 4),
                  Row(
                    children: [
                      // vip标签
                      if (fee == 1) ...[
                        Container(
                          padding: const EdgeInsets.symmetric(
                            vertical: 0,
                            horizontal: 6,
                          ),
                          margin: const EdgeInsets.only(right: 4),
                          decoration: BoxDecoration(
                            border: Border.all(
                              color: const Color(0xFFf43f3b),
                              width: 1,
                            ),
                            borderRadius: BorderRadius.circular(4),
                          ),
                          child: const Text(
                            'VIP',
                            style: TextStyle(
                              color: Color(0xFFf43f3b),
                              fontSize: 8,
                              height: 1.5,
                            ),
                          ),
                        )
                      ],
                      // 试听
                      if (fee == 8) ...[
                        Container(
                          padding: const EdgeInsets.symmetric(
                            vertical: 0,
                            horizontal: 6,
                          ),
                          margin: const EdgeInsets.only(right: 4),
                          decoration: BoxDecoration(
                            border: Border.all(
                              color: const Color(0xFFf43f3b),
                              width: 1,
                            ),
                            borderRadius: BorderRadius.circular(4),
                          ),
                          child: const Text(
                            'VIP',
                            style: TextStyle(
                              color: Color(0xFFf43f3b),
                              fontSize: 8,
                              height: 1.5,
                            ),
                          ),
                        ),
                        Container(
                          padding: const EdgeInsets.symmetric(
                            vertical: 0,
                            horizontal: 6,
                          ),
                          margin: const EdgeInsets.only(right: 4),
                          decoration: BoxDecoration(
                            border: Border.all(
                              color: const Color(0xFFf43f3b),
                              width: 1,
                            ),
                            borderRadius: BorderRadius.circular(4),
                          ),
                          child: const Text(
                            '试听',
                            style: TextStyle(
                              color: Color(0xFFf43f3b),
                              fontSize: 8,
                              height: 1.5,
                            ),
                          ),
                        )
                      ],
                      Expanded(
                        child: Text(
                          artist,
                          maxLines: 1,
                          style: TextStyle(
                            fontSize: 12,
                            color: Colors.grey.shade600,
                          ),
                          overflow: TextOverflow.ellipsis,
                        ),
                      ),
                    ],
                  )
                ],
              ),
            ),
            GestureDetector(
              onTap: () {
                showModalBottomSheet(
                  context: context,
                  backgroundColor: Colors.transparent,
                  isScrollControlled: true,
                  builder: (_) => SongActionBottomSheet(
                    title: title,
                    artist: artist,
                    imageUrl: imageUrl,
                  ),
                );
              },
              child: const Icon(Icons.more_vert),
            )
          ],
        ),
      ),
    );
  }
}
